package com.ruoyi.contract.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.contract.domain.ConBusiness;
import com.ruoyi.contract.domain.ConObjOut;
import com.ruoyi.contract.domain.VCtmsClient;
import com.ruoyi.contract.mapper.ConObjOutMapper;
import com.ruoyi.contract.service.IConObjOutService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by zhangxiang on 2021/9/26.
 */
@Service
public class ConObjOutServiceImpl extends ServiceImpl<ConObjOutMapper, ConObjOut> implements IConObjOutService {

    @Autowired
    ConObjOutMapper conObjOutMapper;

    @Override
    public List<String> getCusIdListByConIdAndTypes(Long conId) {
        return conObjOutMapper.getCusIdsByConId(conId);
    }

    @Override
    public AjaxResult saveObjOut(VCtmsClient cusData, ConBusiness conBusiness) {
        ConObjOut conObjOut = new ConObjOut();
        conObjOut.setConId(conBusiness.getConId());
        if(cusData.getCustomerGkey()!=null){
            conObjOut.setCustomerGkey(cusData.getCustomerGkey());
        }else{
            return AjaxResult.error("无此外部对象权限");
        }
        return this.save(conObjOut)?AjaxResult.success():AjaxResult.error();
    }

    @Override
    public List<ConObjOut> listConObjOutByConId(Long conId) {
        return conObjOutMapper.getConObjectByConId(conId);
    }

    @Override
    public boolean cloneObjOut(Long oldConId, Long newConId) {
        List<ConObjOut> conObjOutList = this.list(new QueryWrapper<ConObjOut>().lambda().eq(ConObjOut::getConId,oldConId));
        for(int i=0;i<conObjOutList.size();i++){
            conObjOutList.get(i).setConId(newConId);
            conObjOutList.get(i).setConObjId(null);
        }
        return this.saveBatch(conObjOutList);
    }

}
